package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.IsDriving
import com.cheyun.netsalev3.bean.Version
import com.cheyun.netsalev3.bean.basedata.BaseDataParam
import com.cheyun.netsalev3.bean.dms.DmsUser
import com.cheyun.netsalev3.bean.login.LoginParam
import com.cheyun.netsalev3.bean.sys.Prdset
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.reflect.KFunction0
import kotlin.reflect.KFunction1


class LoginRepository {
    fun Login(mobile: String, password: String, KFunction0: KFunction1<@ParameterName(name = "param") LoginParam, Unit>, method: (@ParameterName(name = "skey") String) -> Unit) {
        var pp = CommonFunc.textToMD5L32(password)
        ApiClient.getInstance().Login(mobile, pp)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<LoginParam>>() {
                    override fun onSuccess(t: BaseResp<LoginParam>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {
                        if(ex.getSkey().equals("api.v4.login.mobile.channel")){
                            ex.getSkey()?.apply(method)
                        }
                    }
                })
    }


    fun Login2(mobile: String, password: String, method: () -> Unit, kFunction0: KFunction0<Unit>,goDm: (@ParameterName(name = "skey") String) -> Unit) {
        ApiClient.getInstance().Login(mobile, CommonFunc.textToMD5L32(password))
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<LoginParam>>() {
                    override fun onSuccess(t: BaseResp<LoginParam>) {
                        t.data?.cauthkey?.apply {
                            MySharedPreferences.putCauthkey(this)
                            MySharedPreferences.putMobile(mobile)
                            MySharedPreferences.putPassword(password)
                            MySharedPreferences.putLoginData(t.data!!)
                        }
                        if(t.data?.member?.uid != 0){
                            t.data?.member?.workbench?.apply {
                                MySharedPreferences.setWorkbench(this)
                            }
                            t.data?.member?.uconfig?.notice?.apply {
                                MySharedPreferences.putNotice(this)
                            }
                            basData()
                            method()
                        }else{
                            method()
                        }
                    }

                    override fun onError(ex: ApiException) {
                        if(!ex.getSkey().equals("api.v4.login.mobile.channel") ){
                            kFunction0()
                        }else{
                            ex.getSkey()?.apply(goDm)
                        }
                    }
                })

    }

    fun simpleLogin(mobile: String, password: String, method: () -> Unit, kFunction0: KFunction0<Unit>) {
        ApiClient.getInstance().Login(mobile, CommonFunc.textToMD5L32(password))
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<LoginParam>>() {
                    override fun onSuccess(t: BaseResp<LoginParam>) {
                        t.data?.cauthkey?.apply {
                            MySharedPreferences.putCauthkey(this)
                            MySharedPreferences.putMobile(mobile)
                            MySharedPreferences.putPassword(password)
                            MySharedPreferences.putLoginData(t.data!!)
                        }
                        method()
                    }
                    override fun onError(ex: ApiException) {
                        kFunction0()
                    }

                })

    }

    /**
     * 获取基础数据
     */
    fun basData() {
        ApiClient.getInstance().baseData(ApiStores.ATTRIBUTE)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<BaseDataParam>>() {
                    override fun onSuccess(t: BaseResp<BaseDataParam>) {
                        t.data?.apply {
                            MySharedPreferences.putBaseData(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })

//        ApiClient.getInstance().isDriving()
//                .subscribeOn(Schedulers.io())
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .subscribe(object : ApiCallback<BaseResp<IsDriving>>() {
//                    override fun onSuccess(t: BaseResp<IsDriving>) {
//                        t.data?.apply {
//                            MySharedPreferences.putIsDrive(this.is_driving)
//                        }
//                    }
//
//                    override fun onError(ex: ApiException) {
//                        MySharedPreferences.putIsDrive(0)
//                    }
//                })
    }

    /**
     * 获取当前服务器最新版本
     */
    fun getVersion(version: String, platform: String, method: KFunction1<Version, Unit>){
        ApiClient.getInstance().getVersion(version,platform)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Version>>() {
                    override fun onSuccess(t: BaseResp<Version>) {
                        t.data?.apply(method)
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    /**
     * 发送验证码
     */
    fun getPhoneCode(ckey:String,akey:String,mobile: String, KFunction0: KFunction1<@ParameterName(name = "param") Any, Unit>) {
        ApiClient.getInstance().sendSms(ckey,akey,mobile)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    /**
     * 重置密码
     */
    fun forget(mobile:String,password:String,secode: String, KFunction0: KFunction1<@ParameterName(name = "param") Any, Unit>) {
        var pp = CommonFunc.textToMD5L32(password)
        ApiClient.getInstance().forget(mobile,pp,secode)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    /**
     * 修改密码
     */
    fun changePwd(oldpassword:String,password:String,password2: String, KFunction0: KFunction1<@ParameterName(name = "param") Any, Unit>) {
        var pp = CommonFunc.textToMD5L32(password)
        ApiClient.getInstance().changePwd(oldpassword,password,password2)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun editProfile(sex:String,realname:String,birthday: String,headimg: String,staffnum: String,email: String,idnumber: String, method: () -> Unit) {
        ApiClient.getInstance().editProfile(sex,realname,birthday,headimg,staffnum,email,idnumber)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        method()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }
    /**
     * 账户注册
     */
    fun register(mobile:String,password:String,realname:String,secode: String,drcode:String,
                 idnumber:String, KFunction0: KFunction1<@ParameterName(name = "param") Any, Unit>) {
        var pp = CommonFunc.textToMD5L32(password)
        ApiClient.getInstance().register(mobile,pp,realname,secode,drcode,idnumber)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    /**
     * 账户删除
     */
    fun delaccount(mid:String,mobile:String,smscode:String, method: () -> Unit) {
        ApiClient.getInstance().delaccount(mid,mobile,smscode)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        method()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    /**
     * 绑定经销商
     */
    fun bindDistributor(drcode:String,KFunction0: KFunction1<@ParameterName(name = "param") Any, Unit>) {
        ApiClient.getInstance().joinDistributor(drcode)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    /**
     * 获取经销商员工信息
     */
    fun getDmsUser(ickey:String, KFunction0: KFunction1<@ParameterName(name = "param") List<DmsUser>, Unit>) {
        ApiClient.getInstance().getDMSUser(ickey)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<List<DmsUser>>>() {
                    override fun onSuccess(t: BaseResp<List<DmsUser>>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    /**
     * 加入经销商
     */
    fun joinDistributor(ickey:String,UserName:String, KFunction0: KFunction1<@ParameterName(name = "param") Any, Unit>) {
        ApiClient.getInstance().joinDistributor(ickey)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    /**
     * 获取功能切换
     */
    fun getPrdset(KFunction0: KFunction1<@ParameterName(name = "param") List<Prdset>, Unit>) {
        ApiClient.getInstance().getprdset()
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<List<Prdset>>>() {
                    override fun onSuccess(t: BaseResp<List<Prdset>>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }


}